<?php
class Cash_record_model extends CI_Model
{
    private $table = 'cash_record';
    
    /**
     * 记录现金流
     * @param unknown $orderProfitItem
     * @param unknown $type
     */
    public function insertIntoCashRecord($order_id, $uid, $account, $type, $account_type=1, $product_type='line')
    {
        $data = array(
            'order_id'     => $order_id,
            'uid'          => $uid,
            'amount'       => $account,
            'level'        => $type,
            'account_type' => $account_type,
            'time'         => time(),
            'product_type' => $product_type
        );
        $this->db->insert($this->table, $data);
        return $this->db->insert_id();
    }
    
    public function total($search_param=array())
    {
        $this->db->from($this->table.' As cash_record');
        if (!empty($search_param['order_id'])) {
            $this->db->where('cash_record.order_id', $search_param['order_id']);
        }
        if (!empty($search_param['uid'])) {
            $this->db->where('cash_record.uid', $search_param['uid']);
        }
        if (!empty($search_param['account_type'])) {
            $this->db->where('cash_record.account_type', $search_param['account_type']);
        }
        if (!empty($search_param['level_type'])) {
            $this->db->where('cash_record.level', $search_param['level_type']);
        }
        if (!empty($search_param['start_date'])) {
            $this->db->where(array('cash_record.time >' => strtotime($search_param['start_date'])));
        }
        if (!empty($search_param['end_date'])) {
            $this->db->where(array('cash_record.time <=' => strtotime($search_param['end_date'])));
        }
    
        return $this->db->count_all_results();
    }
    
    public function page_list($page_num, $num, $search_param=array())
    {
        $this->db->select('*');
        $this->db->from($this->table.' As cash_record');
        if (!empty($search_param['order_id'])) {
            $this->db->where('cash_record.order_id', $search_param['order_id']);
        }
        if (!empty($search_param['uid'])) {
            $this->db->where('cash_record.uid', $search_param['uid']);
        }
        if (!empty($search_param['account_type'])) {
            $this->db->where('cash_record.account_type', $search_param['account_type']);
        }
        if (!empty($search_param['level_type'])) {
            $this->db->where('cash_record.level', $search_param['level_type']);
        }
        if (!empty($search_param['start_date'])) {
            $this->db->where(array('cash_record.time >' => strtotime($search_param['start_date'])));
        }
        if (!empty($search_param['end_date'])) {
            $this->db->where(array('cash_record.time <=' => strtotime($search_param['end_date'])));
        }
        $this->db->order_by('order_id','desc');
        $this->db->limit($page_num, $num);
    
        return $this->db->get();
    }
}